package com.koro.tool;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

public class LocalDateTimeUtil {

    public static final DateTimeFormatter yyyyMMdd = DateTimeFormatter.ofPattern("yyyy-MM-dd");
    public static final DateTimeFormatter yyyyMMdd1 = DateTimeFormatter.ofPattern("yyyyMMdd");
    public static final DateTimeFormatter yyyyMMddHHmm =
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm");
    public static final DateTimeFormatter yyyyMMddHHmmss =
            DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

    public static LocalDate parseDate(String text) {
        try {
            return LocalDate.parse(text, yyyyMMdd);
        } catch (Exception e) {
            return null;
        }
    }

    public static LocalDate parseLocalDate(String text) {
        try {
            return LocalDate.parse(text, yyyyMMdd1);
        } catch (Exception e) {
            return null;
        }
    }

    public static LocalDateTime parseDateTime(String text) {
        try {
            return LocalDateTime.parse(text, yyyyMMddHHmmss);
        } catch (Exception e) {
            return null;
        }
    }

    public static LocalDateTime parseDateTimeWithoutSecond(String text) {
        try {
            return LocalDateTime.parse(text, yyyyMMddHHmm);
        } catch (Exception e) {
            return null;
        }
    }

    public static String parseLocalDateTime(final String format, final LocalDateTime localDateTime) {
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern(format);
        try {
            return dtf2.format(localDateTime);
        } catch (Exception e) {
            return null;
        }
    }

    public static String parseLocalDate(final String format, final LocalDate localDate) {
        DateTimeFormatter dtf2 = DateTimeFormatter.ofPattern(format);
        try {
            return dtf2.format(localDate);
        } catch (Exception e) {
            return null;
        }
    }


    /**
     * 判断两个时间 前者是否在后者之前
     *
     * @return
     */
    public static Boolean isBefore(LocalDate d1, LocalDate d2) {
        if (null == d1 || null == d2) {
            return false;
        }
        String s1 = d1.format(yyyyMMdd);
        String s2 = d2.format(yyyyMMdd);
        if (s1.equals(s2)) {
            return true;
        }
        if (d1.isBefore(d2)) {
            return true;
        }
        return false;
    }


}